package com.briup.cms.service.impl;
import com.briup.cms.bean.Facility;
import com.briup.cms.dao.FacilityMapper;
import com.briup.cms.dao.extend.FacilityExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IFacilityService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class FacilityServiceImpl implements IFacilityService {
    @Resource
    private FacilityMapper facilityMapper;
    @Resource
    private FacilityExtendMapper facilityExtendMapper;

    @Override
    public PageInfo<Facility> pageQuery(int pageNum, int pageSize, String keywords) {

        PageHelper.startPage(pageNum, pageSize, true);
        List<Facility> list = this.facilityExtendMapper.pageQuery(keywords);
        return new PageInfo(list);

    }
    @Override
    public List<Facility>findAll(){
        return facilityExtendMapper.findAll();
    }
    @Override
    public void saveOrUpdate(Facility facility){
        if(facility.getId()!=null){
            facilityMapper.updateByPrimaryKey(facility);
        }else{
            facilityMapper.insert(facility);
        }
    }

    @Transactional
    public void deleteById(int id) throws CustomerException {
        this.facilityMapper.deleteByPrimaryKey(Integer.valueOf(id));
    }


    @Transactional
    public void batchDelete(int[] ids) throws CustomerException {
        if (ids != null && ids.length > 0)
            for (int id : ids)
                this.facilityMapper.deleteByPrimaryKey(Integer.valueOf(id));
    }
    @Transactional
    public void checkFacility(int id, int status) throws CustomerException {
        Facility facilityFromDB = this.facilityMapper.selectByPrimaryKey(Integer.valueOf(id));
        if (facilityFromDB == null)
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_FACILITY_NOT_EXIST);
        Facility facility = new Facility();
        facility.setStatus(String.valueOf(status));
        this.facilityMapper.updateByPrimaryKeySelective(facility);
    }


}
